package com.api.shihua.util;

import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import weaver.general.BaseBean;
import weaver.general.GCONST;

/**
 * * 
 *  Copyright (C), 2010,9, szcport 
 *  File name and path : szcport.weaver.formmode.data.ModeDataManager.java
 *  Author : KangMD,    Version : 1.0,    First complete date : 2018-5-14 下午05:01:55  
 *  Description :日志类         
 *  Others:                                                     
 *  Function List:        
 *  History:             
 *    1. Date:
 *       Version:
 *       Author:
 *       Modification:
 *    
 **
 */
public class LogTool {
	
    private PrintWriter logPrint;
    private String logFile = "";
    private String logPath = "";
    private boolean systemlog = false;//是否写系统日记
    
    static SimpleDateFormat newDf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    static BaseBean log = new BaseBean();
    
    public LogTool(){
   	 //if(logFile == null || logFile.trim().equals("") || !logFile.equals(getLogFile())){
          //  newLog();
        //}
    }
    
    public LogTool(String logPath,boolean systemlog){
    	this.logPath = logPath;
    	this.systemlog = systemlog;
    }
    
    private String getLogFile(){
    	//获取当前系统路径
    	String sysPath = GCONST.getRootPath();
    	if (sysPath == null) {
    		sysPath = System.getProperty("user.dir");
    	}
    	if (!"".equals(logPath)) {
    		if (logPath.endsWith("/")) {
    			sysPath += logPath+df.format(new Date())+".log";
    		} else {
    			sysPath += logPath+"/"+df.format(new Date())+".log";
    		}
    	} else {
    		sysPath += "/log/dev/"+df.format(new Date())+".log";
		}
        return sysPath;
    }
   
    private void newLog(){
        logFile = getLogFile();
        try {
            logPrint = new PrintWriter(new FileWriter(logFile, true), true);
        } catch(IOException e) {
            try {
            	 File file = new File(logFile);
            	 if (!file.getParentFile().exists()) {  
                     //如果目标文件所在的目录不存在，则创建父目录  
                     if (file.getParentFile().mkdirs()) {
                    	 file.createNewFile();
                     }
                 }  
                logPrint = new PrintWriter(new FileWriter(logFile, true), true);
            } catch(IOException ex) {
            	log.writeLog("Log记录出错了",ex);
            }
        }
    }

    public void writeLog(Object msg) {
    	if (systemlog) {
    		log.writeLog(msg);
    	}
    	newLog();
        logPrint.println(newDf.format(new Date()) + ": " + msg);
        logPrint.flush();
        logPrint.close();
    }
   
    public void writeLog(String msg,Throwable e) {
    	if (systemlog) {
    		log.writeLog(msg,e);
    	}
    	newLog();
        logPrint.println(newDf.format(new Date()) + ": " + msg);
        e.printStackTrace(logPrint);
        logPrint.flush();
        logPrint.close();
    }
    
    public static void main(String[] args) {
    	try {
			Double.parseDouble("aaa");
		} catch (Exception e) {
			new LogTool().writeLog("哈哈111哈",e);
		}
	}
    
}